//index.js
const app = getApp()
const host = require('../../config.js').host;
const network = require('../../utils/network.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    host: host,
    pageNum:1,
    userData: {},
    userInfo: {},
    dataList: []
  },

  getData() {
    let that = this;
    network.requestGet(host + '/xcx/game/score-info', {}, res => {
      
      if (res.data.code == 200) {
        that.setData({
          userData: res.data.data,
        })
      } else {
        wx.showToast({
          title: res.data.msg ? res.data.msg : '请求失败',
          icon: "none",
          duration: 1200
        })
      }
    })
  },

  getInfo() {
    let that = this;
    network.requestGet(host + '/xcx/customer/getUserInfo', {}, res => {
      
      if (res.data.code == 200) {
        that.setData({
          userInfo: res.data.data, 
        }) 
      } else {
        wx.showToast({
          title: res.data.msg ? res.data.msg : '请求失败',
          icon: "none",
          duration: 1200
        })
      }
    })
  },

  // switchFold(e) {
  //   var index = e.currentTarget.dataset.index;
  //   var list = this.data.list;
  //   if (list[index].isShow) {
  //     list[index].isShow = false
  //   } else {
  //     list[index].isShow = true
  //   }瑞立夫
  //   this.setData({
  //     list: list,
  //   })
  // },
  getList() {
    var timestamp = Date.parse(new Date());   
    timestamp = timestamp / 1000;     
    if(timestamp<1621761630){
      this.setData({
        dataList: [
          {
            readFlag:0,
            messageTitle:'系统消息',
            messageContent:'亲爱的玩家，平台现在已经新上了多款刺激好玩的游戏，还能获得更多的礼物和优惠券等惊喜，快来邀请您的好友一起去试玩吧~',
            hasOpen:false, 
          },
          {
            readFlag:0,
            messageTitle:'游戏提示',
            messageContent:'如果亲在游戏中遇到任何的问题，请尽快联系我们，我们会尽快的解决您的问题，谢谢！',
            hasOpen:false, 
          },
          {
            readFlag:0,
            messageTitle:'恭喜您',
            messageContent:'感谢亲成为本平台的用户，祝您玩的开心！',
            hasOpen:false, 
          },
        ]
      })
      return;
    }
    let that = this;
    network.requestGet(host + '/xcx/systemMessage/message-list', {
      "pageSize": "10",
      "pageNum": that.data.pageNum,
      "from": "1"
    }, res => {
      if (res.data.code == 200) { 
        if (that.data.pageNum == 1) {
          that.setData({
            dataList: res.data.rows
          })
        } else {
          that.setData({
            dataList: that.data.dataList.concat(res.data.rows)
          })

        }
        wx.stopPullDownRefresh();
      } else {
        wx.showToast({
          title: res.data.msg ? res.data.msg : '请求失败',
          icon: "none",
          duration: 1200
        })
      }
    })
  },
  messageSelected(e) {
    console.log('===='+e.currentTarget.dataset.index)
    
    let dataList = this.data.dataList;
    
    dataList[e.currentTarget.dataset.index].hasOpen = !dataList[e.currentTarget.dataset.index].hasOpen;
    this.setData({
      dataList: dataList
    })

    if(dataList[e.currentTarget.dataset.index].readFlag == 0){
      let that = this;
      network.requestPost(host + '/xcx/messageReadedLog/addMessageReadedLog', {
        "messageId": dataList[e.currentTarget.dataset.index].id,
        "messageType": "1" 
      }, res => {
        if (res.data.code == 200) { 
          dataList[e.currentTarget.dataset.index].readFlag = 1;
          that.setData({
            dataList: dataList
          }) 
        } else {
          // wx.showToast({
          //   title: res.data.msg ? res.data.msg : '请求失败',
          //   icon: "none",
          //   duration: 1200
          // })
        }
      })
    } 
  },
  toRank() {
    wx.navigateTo({
      url: '/pages/rank/rank',
    })
  },
  gotocurrency(){
    wx.navigateTo({
      url: '/pages/currency/currency',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList() 
  },
  gotoscan(){
    wx.scanCode({
      success: res => {
        
        if (res.path) {
           
          wx.reLaunch({
              url: res.path
          })
        }
    }
      // success (res) {
      //   debugger
      //   console.log('成功:'+res.path)//deviceSeatSn=1289639813
      //   if(res.path.indexOf('pages/gamedetail/gamedetail') != -1){
      //     wx.navigateTo({
      //       url:res.path
      //       // url: '/pages/gamedetail/gamedetail?deviceSeatSn='+res.result,
      //     })
      //   }
        
      // }
    
    })
    // wx.navigateTo({
    //   url: '/pages/gamedetail/gamedetail?seatSn='+"841753812860930",
    // })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // let token = wx.getStorageSync('token');
    this.getData();
    this.getInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

   /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pageNum:1
    })
    this.getList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var pageNum=this.data.pageNum+1
    this.setData({
      pageNum:pageNum
    })
    this.getList();
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})